package com.learn.service.impl;

import com.learn.entity.BookEntity;
import com.learn.utils.RRException;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.learn.service.SysUserService;


import com.learn.dao.RecordDao;
import com.learn.entity.RecordEntity;
import com.learn.service.RecordService;
import com.learn.service.*;


@Service("recordService")
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordDao recordDao;


    @Autowired
    private BookService bookService;


    @Autowired
    private SysUserService sysUserService;


    @Override
    public RecordEntity queryObject(Long id) {
        RecordEntity entity = recordDao.queryObject(id);


        if (entity.getBook() != null && this.bookService.queryObject(entity.getBook()) != null)
            entity.setBookEntity(this.bookService.queryObject(entity.getBook()));

        if (this.sysUserService.queryObject(entity.getUser()) != null)
            entity.setSysUserEntity(this.sysUserService.queryObject(entity.getUser()));


        return entity;
    }

    @Override
    public List<RecordEntity> queryList(Map<String, Object> map) {
        List<RecordEntity> list = recordDao.queryList(map);
        for (RecordEntity entity : list) {

            if (entity.getBook() != null && this.bookService.queryObject(entity.getBook()) != null)
                entity.setBookEntity(this.bookService.queryObject(entity.getBook()));

            if (this.sysUserService.queryObject(entity.getUser()) != null)
                entity.setSysUserEntity(this.sysUserService.queryObject(entity.getUser()));

        }


        for(RecordEntity entity :this.recordDao.queryList(new HashMap<String, Object>())){

            if(System.currentTimeMillis() >entity.getEnd().getTime() && "未归还".equals(entity.getState())){
                entity.setMoney("20元");
                this.recordDao.update(entity);
            }

        }

        return list;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return recordDao.queryTotal(map);
    }

    @Override
    public void save(RecordEntity record) {
        //借出
        BookEntity book = this.bookService.queryObject(record.getBook());
        if (!"在库".equals(book.getState())) {
            throw new RRException("产品已被借出");
        }

        book.setState("已借出");
        this.bookService.update(book);
        record.setBegin(new Date());
        //默认15天内还
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 15);
        record.setEnd(cal.getTime());
        record.setState("未归还");


        recordDao.save(record);
    }

    @Override
    public void update(RecordEntity record) {


        Map<String, Object> map = new HashMap<>();
        map.put("book", record.getBook());
        map.put("end", "1");
        if (this.recordDao.queryList(map).size() == 0) {
            throw new RRException("未查询到借出记录");
        }

        RecordEntity entity = this.recordDao.queryList(map).get(0);
        if (!entity.getUser().equals(record.getUser())) {
            throw new RRException("请勿操作他人的书籍");
        }

        entity.setSend(new Date());
        entity.setState("已归还");


        BookEntity book = this.bookService.queryObject(record.getBook());
        book.setState("在库");
        this.bookService.update(book);

        recordDao.update(entity);
    }

    @Override
    public void delete(Long id) {
        recordDao.delete(id);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        recordDao.deleteBatch(ids);
    }

}
